package com.controller;

import com.mapper.BankMapper;
import com.model.ResponseMsg;
import com.model.User;
import com.model.UserBank;
import com.util.CheckBankCard;
import com.util.ConstantUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@PropertySource("classpath:message.properties")
public class BankController {

    @Autowired
    private BankMapper bankMapper;

    @Autowired
    private Environment env;


    // inject via application.properties

    @Value("${welcome.message:test}")
    private String message = "Hello World";

    @RequestMapping("/banklist")
    public String userBankList(Map<String, Object> model, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(ConstantUtil.loginSession);
        UserBank bank = new UserBank();
        bank.setUserId(user.getUserId());
        List<UserBank> bankList = bankMapper.getBanksByCondition(bank);
        model.put("list",bankList);
        model.put("pagename","banklist");

        return "usercenter";
    }

    @RequestMapping("/deleteBank")
    @ResponseBody
    public ResponseMsg deleteBank(@RequestParam(required = true) String bankId) {
        ResponseMsg msg = new ResponseMsg();

        bankMapper.deleteBankById(bankId);
        msg.setData("");
        msg.setStatus(ResponseMsg.success);
        return msg;
    }


    @RequestMapping("/addBank")
    @ResponseBody
    public ResponseMsg addBank(HttpServletRequest request) {
        String bankName = request.getParameter("bankName");
        String bankNo = request.getParameter("bankNo");
        String branchBankName = request.getParameter("branchBankName");
        String city = request.getParameter("city");
        String district = request.getParameter("district");
        String province = request.getParameter("province");
        String userName = request.getParameter("userName");
        ResponseMsg msg = new ResponseMsg();
        if(StringUtils.isBlank(bankNo) || StringUtils.isBlank(bankName) || StringUtils.isBlank(userName) || StringUtils.isBlank(branchBankName)){
            msg.setData("参数错误！");
            return msg;
        }

        if(!CheckBankCard.checkBankCard(bankNo.trim())){
            msg.setData("银行卡号有问题！");
            return msg;
        }

        User user = (User) request.getSession().getAttribute(ConstantUtil.loginSession);
        UserBank bank = new UserBank();
        bank.setUserName(userName);
        bank.setBankName(bankName);
        bank.setBankNo(bankNo);
        bank.setBranchBankName(branchBankName);
        bank.setCity(city);
        bank.setBankNo(bankNo);
        bank.setBranchBankName(branchBankName);
        bank.setDistrict(district);
        bank.setProvince(province);
        bank.setUserId(user.getUserId());

        bankMapper.addBank(bank);

        msg.setStatus(ResponseMsg.success);
        return msg;
    }


    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }


    public BankMapper getBankMapper() {
        return bankMapper;
    }

    public void setBankMapper(BankMapper bankMapper) {
        this.bankMapper = bankMapper;
    }

    public Environment getEnv() {
        return env;
    }

    public void setEnv(Environment env) {
        this.env = env;
    }

}
